<%@ page import="com.oval.util.JspHelper" %>
<%@ page contentType = "text/html;charset=UTF-8" %>
<%@ page language="java" %>     
<%@ page import="java.util.*" %>
<%@ page import= "org.apache.log4j.*"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/custom.tld" prefix="custom" %>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../css/chinese/csscontent.css" type="text/css">

<%  
    String logname = "";
    if(request.getParameter("logname")!=null)
      logname = request.getParameter("logname");
    else
      logname = (String)session.getAttribute("logname");
    
    session.setAttribute("logname",logname);
    
    Vector submitV = new Vector();
    int maxlth = 0;
    
    Vector vvvv = new Vector();
    Enumeration enum = Category.getCurrentCategories();
    while(enum.hasMoreElements())  // get the max capacity of the container
    {
      String tmp = ((Category)enum.nextElement()).getName();
      StringTokenizer st = new StringTokenizer(tmp,".");
      int tmplth = st.countTokens();
      maxlth = Math.max(maxlth,tmplth);
    }
    Vector[] vv = new Vector[maxlth+1];  // init the container
    for(int l = 0; l<vv.length; l++)
    {
      vv[l] = new Vector();
    }
    
    int id = 1;
    Enumeration enum2 = Category.getCurrentCategories();
    while(enum2.hasMoreElements())  // category the names according to the layer
    {
      Category a_cat = (Category)enum2.nextElement();
      String tmp = a_cat.getName();
      String fathername = "";
      Level level = a_cat.getLevel();
      Level levelFromHierarchy = a_cat.getEffectiveLevel();
      int level_int = levelFromHierarchy.toInt()/10000;
      StringTokenizer st = new StringTokenizer(tmp,".");
      int tmplth = st.countTokens(); 
      HashMap catinfo = new HashMap();
      catinfo.put("name",tmp);
      catinfo.put("fathername",fathername);
      catinfo.put("level",level);
      catinfo.put("levelFromHierarchy",levelFromHierarchy);
      catinfo.put("level_int",new Integer(level_int));
      catinfo.put("length",new Integer(tmplth));
      catinfo.put("id",new Integer(id));
      catinfo.put("grade",new Integer(1));
      vv[tmplth].add(catinfo);
      id++;
    }
    
    for(int j=1;j<vv.length;j++)  // get every layer order by asc 
    {
      if(vv[j]!=null && vv[j].size()!=0)  
      {
        for(int m = 0 ;m < vv[j].size() ;m++)  // find every item of the layer 
        {
          boolean flag = false;
          HashMap a_catinfo = (HashMap)vv[j].get(m);
          String a_name = (String)a_catinfo.get("name");  // get the current name of the item
          for(int n = j-1 ;n >=0&&!flag;n--)  // find the prelayers order by desc
          {
            for(int o = 0; o< vv[n].size()&&!flag ;o++)  
            {
              HashMap tmp_catinfo = (HashMap)vv[n].get(o);
              String tmp_name = (String)tmp_catinfo.get("name"); // the item in the prelayer
              int tmp_grade = ((Integer)tmp_catinfo.get("grade")).intValue();
              Integer tmp_id = (Integer)tmp_catinfo.get("id");
              if((a_name.trim()).startsWith(tmp_name.trim()))
              {
                a_catinfo.put("fathername",tmp_name);
                a_catinfo.put("fatherid",tmp_id);
                int m_grade = tmp_grade+1;
                a_catinfo.put("grade",new Integer(m_grade));
                vvvv.add(vvvv.indexOf(tmp_catinfo)+1,a_catinfo); //add the name of the layer just after one of the prelayer 
                flag = true;
              } 
            }
          }
          if(!flag)  vvvv.add(a_catinfo); // add the name of the layer in the end of the container
        }
      }
    }
   Vector vvv = new Vector();
   for(int i =0 ; i<vvvv.size();i++)
   {
     HashMap h = (HashMap)vvvv.get(i);
  
     if(((String)h.get("name")).equals(logname))
     {
       vvv.add(0,h);
     }
     
     if(h.get("fathername")!=null && ((String)h.get("fathername")).equals(logname))
     {
       vvv.add(h);
     }
   }
%>


</head>
<body bgcolor="#FFFFFF" text="#000000" >

<table width="100%" border="0" cellspacing="1" cellpadding="2" bgcolor="#999999" >
<tr height="30"  align="center"> 
      <td class="td_title_bg" width="40%">
         <b>Category Name</b>
      </td>
      <td class="td_title_bg" >
         <b># Current Log Level</b>
      </td>
      <td class="td_title_bg" >
         <b># Current Level From Hierarchy</b>
      </td>
      <td class="td_title_bg">
         <b># Change Log Level</b>
      </td>

</tr>
<form name="cform" method="post" action="../../Dispatcher">   
  <custom:hidden property="pagename" value="SetCategory"/>
  <custom:hidden property="actionname"/>

<% int length = 0;%>
<logic:iterate id="catinfo" collection="<%= vvv %>" indexId="catid">
<tr class="td_bg_row" height="25">
  <td class="td_left">&nbsp;<% length = ((Integer)((HashMap)catinfo).get("length")).intValue();
     for(int k =0 ;k<=length; k++) {%>&nbsp;&nbsp;&nbsp;<%}%>
     <custom:write name ="catinfo" property="name"/><custom:hidden name ="catinfo" property="name"/></td>
  <td class="td_center">&nbsp;<custom:write name ="catinfo" property="level"/></td>
  <td class="td_center">&nbsp;<custom:write name ="catinfo" property="levelFromHierarchy"/>
                              <custom:hidden name ="catinfo" property="level"/>
                              <custom:hidden name ="catinfo" property="levelFromHierarchy"/>
                              <custom:hidden name ="catinfo" property="level_int"/>
                              <custom:hidden name ="catinfo" property="fathername"/></td>
  <td class="td_center">&nbsp;
  <select name="levelChanged" onChange="javascript:change('<%=JspHelper.filterNull( catid.intValue())%>')">
    <option value="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
    <option value="5">&nbsp;&nbsp;Fatal&nbsp;&nbsp;</option>
    <option value="4">&nbsp;&nbsp;Error&nbsp;&nbsp;</option>
    <option value="2">&nbsp;&nbsp;Info&nbsp;&nbsp;</option>
    <option value="1">Debug</option>
  </select>
  </td>
  
</tr>    
</logic:iterate>
</form>
<tr>
 <table width="100%" border="0" align="center">
      <tr></tr>
      <tr></tr>
      <tr></tr>
      <tr></tr>
      <tr></tr>
      <tr></tr>
      <tr>
        <td>
          <div align="center">
            <input type="image" border="0" name="imageField" src="../images/chinese/bt-done.gif" width="57" height="18" onClick="done()">
          </div>
        </td>
      </tr>
    </table>
</tr>    
</table>
</body>
<script language="JavaScript">
function done()
{
  cform.actionname.value = "setcategorybatch";
  cform.target = "_self";
  cform.submit();
}

function change(id){
  var id1 = id;  
  var namelength = cform.name.length;
  if ( typeof(namelength) != "undefined")
  {
    var name =  cform.name[id1].value;
    var level =  cform.levelChanged[id1].value;
    var level_int =  cform.level_int[id1].value;
    
    var id2 = id;
    var id2 = ++id2;
  
    while(id2<namelength)
    {
      var name2 =  cform.name[id2].value;
      var level2 =  cform.levelChanged[id2].value;
      var father2 = cform.fathername[id2].value;
      var level_int2 =  cform.level_int[id2].value;
      if (father2 == name)
      {
        if(level == 6)  
        {
          if(level2 == 6)
            cform.levelChanged[id2].value = Math.max(level_int2,level_int);    
          else
            cform.levelChanged[id2].value = Math.max(level2,level_int);
        }
        else if(level2 == 6)
          cform.levelChanged[id2].value = Math.max(level_int2,level);
        else
          cform.levelChanged[id2].value = Math.max(level2,level);
        id2++;
      }
      else
      {
        break;
      }  
    }
  }
}

      


</script>
</html>


